﻿using Forum.Models;
using ForumBL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Forum.Controllers
{
    public class PostController : Controller
    {

        public ActionResult Index(int id)
        {
            PostBL postContext = new PostBL();
            CategoryBL categoryContext = new CategoryBL();
            ForumDomainModels.Category category = new ForumDomainModels.Category();
            category.Id = id;
            category = categoryContext.GetCategoryById(category);
            GeneralPostModel generalModel = new GeneralPostModel();
            generalModel.CategoryId = category.Id;
            generalModel.CategoryName = category.CategoryName;
            List<ForumDomainModels.Post> posts = postContext.GetPostsByCategory(category);

            foreach (var post in posts)
            {
                PostModel currentPostModel = new PostModel();
                currentPostModel.Id = post.Id;
                currentPostModel.Title = post.Title;
                currentPostModel.DatePublished = post.DatePublished;
                generalModel.postModels.Add(currentPostModel);
            }
            return View(generalModel);
        }

        public ActionResult PostDetail(int id)
        {
            PostBL postContext = new PostBL();
            UserBL userContext = new UserBL();
            ForumDomainModels.Post post = new ForumDomainModels.Post();
            post.Id = id;
            post = postContext.GetPostById(post);
            PostDetailModel postModel = new PostDetailModel();
            postModel.Id = post.Id;
            postModel.Title = post.Title;
            postModel.DatePublished = post.DatePublished;
            postModel.Content = post.Content;

            ForumDomainModels.User creatorPost = new ForumDomainModels.User();
            creatorPost.Id = post.AuthorId;
            creatorPost = userContext.GetUserById(creatorPost);
            postModel.AuthorUsername = creatorPost.Username;
            postModel.Answers = FillAnswersToPostModel(postModel);
            return View(postModel);
        }

        private List<AnswerModel> FillAnswersToPostModel(PostDetailModel postModel)
        {
            AnswerBL answerContext = new AnswerBL();
            ForumDomainModels.Post post = new ForumDomainModels.Post();
            post.Id = postModel.Id;
            List<ForumDomainModels.Answer> answersToPost = answerContext.GetAnswersToPost(post);
            if (answersToPost == null)
            {
                return null;
            }
            else
            {
                List<AnswerModel> answerModels = new List<AnswerModel>();
                foreach (var answer in answersToPost)
                {
                    UserBL userContext = new UserBL();
                    AnswerModel currentAnswer = new AnswerModel();
                    currentAnswer.Content = answer.Content;
                    currentAnswer.DatePublished = answer.DatePublished;
                    ForumDomainModels.User creatorAnswer = new ForumDomainModels.User();
                    creatorAnswer.Id = answer.AuthorId;
                    creatorAnswer = userContext.GetUserById(creatorAnswer);
                    currentAnswer.AuthorUsername = creatorAnswer.Username;
                    answerModels.Add(currentAnswer);
                }
                return answerModels;
            }
        }

        [HttpGet]
        public ActionResult PostCreate(int id)
        {
            CreatePostModel postModel = new CreatePostModel();
            postModel.CategoryId = id;
            return View(postModel);
        }

        [HttpPost]
        public ActionResult PostCreate(CreatePostModel postModel)
        {
            if (ModelState.IsValid)
            {
                PostBL postContext = new PostBL();
                ForumDomainModels.Post post = new ForumDomainModels.Post();
                post.AuthorId = (int)HttpContext.Session["Id"];
                post.CategoryId = postModel.CategoryId;
                post.Content = postModel.Content;
                post.DatePublished = DateTime.Now;
                post.Title = postModel.Title;
                postContext.InsertPost(post);
                return RedirectToAction("Index", "Post", new { id = post.CategoryId });
            }
            else
            {
                ModelState.AddModelError("", "The all fields are required.");
                return View();
            }
        }

    }
}
